package com.lanyou.esb.web.serviceCheck;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.userManagement.User;
import com.lanyou.cook.service.security.UserSessionService;
import com.lanyou.esb.datadictionary.AuditStatusType;
import com.lanyou.esb.repository.ServiceCheckRepository;
import com.lanyou.esb.service.ServiceCheckService;
import com.lanyou.esb.web.param.ServiceReuseParam;
import com.lanyou.esb.web.serviceCheck.vo.ReuseServiceSystemVo;
import com.lanyou.esb.web.serviceCheck.vo.ServiceReuseDegreeVo;

/**
 * 服务重用度
 * 
 * @author Davey.wu
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/serviceReuseDegree")
public class ServiceReuseDegreeController {

	@Autowired
	private ServiceCheckService serviceCheckService;

	@Autowired
	private ServiceCheckRepository serviceCheckRepository;

	@Autowired
	UserSessionService userSessionService;
	/** 时间格式：yyyy-MM-dd HH:mm:ss **/
	private static FastDateFormat FASTDATEFORMAT_YYYYMMDDHHMMSS = FastDateFormat
			.getInstance(Constants.DATETIME_PATTERN_1);

	/**
	 * 获取服务重用度
	 * 
	 * @param param
	 * @param pageable
	 * @return
	 */
	@RequestMapping("/search/findServiceReuseDegreeInfo")
	public Page<ServiceReuseDegreeVo> findServiceReuseDegreeInfo(
			ServiceReuseParam param, Pageable pageable) {
		if (StringUtils.isNotBlank(param.getIfName())) {
			param.setIfName("%" + param.getIfName() + "%");
		}
		User user = userSessionService.getCurrentAuthenticatedUser();
		// 获取服务重用度
		return serviceCheckService.getServiceReuseDegreeInfo(param, pageable,
				user.getId());
	}

	/**
	 * 获取重用服务系统信息
	 * 
	 * @param param
	 * @param pageable
	 * @return
	 */
	@RequestMapping("/search/findServiceReuseSystemInfo")
	public Page<ReuseServiceSystemVo> findServiceReuseSystemInfo(
			ServiceReuseParam param, Pageable pageable) {
		if (StringUtils.isNotBlank(param.getIfName())) {
			param.setIfName("%" + param.getIfName() + "%");
		}

		// 查看总记录数
		List<Object> totals = serviceCheckRepository
				.findServiceReuseSystemTotalNum(param.getIfId());
		BigInteger total = null;
		if (totals != null && totals.size() > 0) {
			total = (BigInteger) totals.get(0);
		}
		if (total == null || total.longValue() <= 0) {
			return new PageImpl<ReuseServiceSystemVo>(
					new ArrayList<ReuseServiceSystemVo>(0), pageable, 0);
		}
		// 获取重用服务系统信息
		List<Object[]> list = serviceCheckRepository
				.findReuseServiceSystemInfo(param.getIfId(),
						pageable.getPageNumber() * pageable.getPageSize(),
						pageable.getPageSize());

		List<ReuseServiceSystemVo> result = new ArrayList<>(list.size());
		for (Object[] objects : list) {
			ReuseServiceSystemVo vo = new ReuseServiceSystemVo();
			vo.setSystemName(objects[0] != null ? (String) objects[0] : "");
			vo.setUserAccount(objects[1] != null ? (String) objects[1] : "");
			vo.setUserName(objects[2] != null ? (String) objects[2] : "");
			result.add(vo);
		}
		Page<ReuseServiceSystemVo> page = new PageImpl<ReuseServiceSystemVo>(
				result, pageable, total.longValue());
		return page;
	}

}
